<?php
/*
 * @分销商城
 * @功能说明：购物车
 * @更新说明：暂无更新
 * @文件名 CartController.class.php
 * @编码 UTF-8
 * @创建时间 2016-3-7 下午3:40:17
 * @创建人 XING
 */

namespace wap\Controller;
class CartController extends ApibaseController
{
    /*
	 * 分销商城_加入购物车
	 * @date: 2016-3-7 下午3:40:40
	 * @editor: XING
	 */
	public function putCart(){
		if(I('get.goods_id') && I('get.nums') && I('get.sek')){
			$goodsDetail = M('GoodsEntity')
						 ->where(array('goods_id'=>I('get.goods_id')))->find();
			if(!$goodsDetail){
				dataFormat('',0,'非法请求');
			}
			$checkGoods = M('ShopCart')
					    ->where(array('goods_id'=>I('get.goods_id'),'cust_id'=>session('loginUid'),'biz_cust_id'=>spotSek(I('get.sek'))))->find();
			$data = array(
				'goods_price' => $goodsDetail['goods_price'],
				'goods_title' => $goodsDetail['goods_title'],
				'goods_pic' => $goodsDetail['goods_pic'],
				'goods_attr' => $goodsDetail['goods_attr'],
			);
			if($checkGoods){
				$data['goods_num'] = I('get.nums') + $checkGoods['goods_num'];
				$shopCarts = M('ShopCart')
						   ->where(array('goods_id'=>I('get.goods_id'),'cust_id'=>session('loginUid'),'biz_cust_id'=>spotSek(I('get.sek'))))->setField($data);
				if($shopCarts){
					dataFormat('',1,'加入购物车成功');
				}else{
					dataFormat('',0,'操作失败');
				}
			}else{
				$data['goods_id'] = I('get.goods_id');
				$data['goods_num'] = I('get.nums');
				$data['biz_cust_id'] = spotSek(I('get.sek'));
				$data['cust_id'] = session('loginUid');
				$data['type'] = I('get.type')=='2'? I('get.type'):'1';
				$data['add_at'] = time();
				$shopCarts = M('ShopCart')->add($data);
				if($shopCarts){
					dataFormat('',1,'加入购物车成功');
				}else{
					dataFormat('',0,'操作失败');
				}
			}
		}else{
			dataFormat('',0,'参数缺失');
		}
	}
	
    /*
	 * 分销商城_购物车页面
	 * @date: 2016-3-10 下午3:40:18
	 * @editor: XING
	 */
	public function index(){
		$sql = 'SELECT * FROM fx_shop_cart INNER JOIN fx_cust_entity 
				ON fx_cust_entity.cust_id = fx_shop_cart.biz_cust_id
				LEFT JOIN fx_goods_entity
				ON fx_shop_cart.goods_id = fx_goods_entity.goods_id
				WHERE fx_shop_cart.cust_id = '.session('loginUid').'
				AND fx_shop_cart.type = 1
				AND online = 1
				ORDER BY fx_shop_cart.add_at DESC';
		$queryData = CQuery($sql);

		$output = $goodsMsg = array();
		if($queryData){
			foreach($queryData AS $val){
				$goodsMsg[$val['biz_cust_id']][] = array(
						'id' => $val['id'],
						'goods_id' => $val['goods_id'],
						'goods_price' => $val['goods_price'],
						'goods_title' => $val['goods_title'],
						'goods_taxes_price' => $val['goods_taxes_price'],
						'goods_pic' => $val['goods_pic'],
						'goods_num' => $val['goods_num'],
						'goods_attr' => $val['goods_attr'],
						'biz' => $val['cust_realname'],
						'biz_cust_id' => $val['biz_cust_id'],
					);
				$output[$val['biz_cust_id']]['goods'] = $goodsMsg[$val['biz_cust_id']];
				$output[$val['biz_cust_id']]['msg'] = array(
							'cust_realname' => $val['cust_realname']
						);
			}
		}
		
		$this->assign('data',$output);
		$this->display('cart/index');
	}
	
    /*
	 * 分销商城_购物车统计
	 * @date: 2016-3-14 下午1:38:42
	 * @editor: XING
	 */
	public function count(){
		if(I('get.params')){
			$info = array(
				'priceNum' => 0,
				'taxesPrice' => 0
			);
			foreach(explode(',',I('get.params')) AS $val){
				$dataVal = explode('_',$val);
				$getGoodsMsg = M('GoodsEntity')->field('goods_price,goods_taxes_price')
						     ->where(array('goods_id'=>$dataVal[0]))->find();
				$info['priceNum'] += $getGoodsMsg['goods_price'] * $dataVal[1];
				$info['taxesPrice'] += $getGoodsMsg['goods_taxes_price'] * $dataVal[1];
			}
			dataFormat($info);
		}else{
			dataFormat('',0,'参数缺失');
		}
	}

	/*
	 * 分销商城_OEM购物车
	 * @date: 2016-05-16 14:01:00
	 * @editor: WENCUN
	 * */
	public function oemIndex(){
		if(I('get.sek') && I('get.type')){
			$getGoodsMsg = M('ShopCart')->field(array('goods_id','goods_pic','goods_price','goods_title','goods_num','goods_attr','biz_cust_id'))
						 ->where(array('cust_id'=>session('loginUid'),'biz_cust_id'=>spotSek(I('get.sek')),'type'=>I('get.type')))->select();
			if($getGoodsMsg){
				dataFormat($getGoodsMsg);
			}else{
				dataFormat('',0,'没有商品');;
			}
		}else{
			dataFormat('',0,'参数缺失');
		}
	}

	/*
	 * 分销商城_OEM购物车编辑
	 * @date: 2016-05-16 16:13:00
	 * @editor: WENCUN
	 * */
	public function oemEdit(){
		$getGoods = M('ShopCart')
				  ->where(array('cust_id'=>session('loginUid'),'goods_id'=>I('get.gid'),'biz_cust_id'=>I('get.bid'),'type'=>'2'))->getField(array('goods_num'));
		if(I('get.type') == '1'){
			$setInc = M('ShopCart')
					->where(array('cust_id'=>session('loginUid'),'goods_id'=>I('get.gid'),'biz_cust_id'=>I('get.bid'),'type'=>'2'))->setInc('goods_num');
			dataFormat($getGoods+1);
		}else{
			if($getGoods == '1'){
				$del = M('ShopCart')
					->where(array('cust_id'=>session('loginUid'),'goods_id'=>I('get.gid'),'biz_cust_id'=>I('get.bid'),'type'=>'2'))->delete();
				dataFormat('',0,'没有数据');
			}else{
				$setInc = M('ShopCart')
					->where(array('cust_id'=>session('loginUid'),'goods_id'=>I('get.gid'),'biz_cust_id'=>I('get.bid'),'type'=>'2'))->setDec('goods_num');
				dataFormat($getGoods-1);
			}
		}
	}

	/*
	 * 分销商城_购物车OEM商品统计
	 * @date: 2016-05-16 13:06:00
	 * @editor: WENCUN
	 * */
	public function oemCount(){
		if(I('get.sek') && I('get.type')){
			$priceNum = 0;
			$getGoodsMsg = M('ShopCart')->field(array('goods_price','goods_num'))
						 ->where(array('cust_id'=>session('loginUid'),'biz_cust_id'=>spotSek(I('get.sek')),'type'=>I('get.type')))->select();
			if($getGoodsMsg){
				foreach($getGoodsMsg as $key=>$value){
					$priceNum += $value['goods_price'] * $value['goods_num'];
				}
				dataFormat($priceNum);
			}else{
				dataFormat('',0,'没有数据');
			}
		}else{
			dataFormat('',0,'参数缺失');
		}
	}

	/*
	 * 分销商城_购物车OEM商品清空
	 * @date: 2016-05-16 17:41:00
	 * @editor: WENCUN
	 * */
	public function oemDelCart(){
		if(I('get.sek')){
			$del = M('ShopCart')->where(array('cust_id'=>session('loginUid'),'biz_cust_id'=>spotSek(I('get.sek')),'type'=>'2'))->delete();
			if($del){
				dataFormat('',1,'已清空');
			}else{
				dataFormat('',0,'操作失败');
			}
		}else{
			dataFormat('',0,'参数缺失');
		}
	}
	
    /*
	 * 分销商城_提交生成订单
	 * @date: 2016-3-14 下午2:43:22
	 * @editor: XING
	 */
	public function putOrder(){
		if(I('get.params')){
			/*校验商品数据信息*/
			//购买限额[未完税商品](年:2W,日:2K,单笔订单:2K)
			$priceNum = 0;
			$year = date('Y',time());
			$today = date('Y-m-d',time());
			$startTime =strtotime($year.'-01-01 00:00:00');
			$endTime =strtotime(($year+1).'-01-01 00:00:00');
			//A:统计一年的购买金额
			$sql = 'SELECT SUM(pay_price*pay_nums) AS moneys
					FROM fx_order_entity a
					LEFT JOIN fx_order_collect b
					ON a.order_id = b.order_id
					WHERE a.order_status IN(0,1,2,3,4,5,6,7,8,9)
					AND b.taxes = 0
					AND cust_id = '.session('loginUid').'
					AND a.add_at >= '.$startTime.'
					AND a.add_at < '.$endTime;
			$getYearSum = CQuery($sql);
			if($getYearSum['moneys'] > 20000){
				dataFormat('',0,'今年购买额度已满');
			}
			//B:统计今日的购买金额
			$sql2 = 'SELECT SUM(pay_price*pay_nums) AS moneys
					FROM fx_order_entity a
					LEFT JOIN fx_order_collect b
					ON a.order_id = b.order_id
					WHERE a.order_status IN(0,1,2,3,4,5,6,7,8,9)
					AND b.taxes = 0
					AND cust_id= '.session('loginUid').'
					AND a.add_at >= '.strtotime($today.' 00:00:00').'
					AND a.add_at <= '.strtotime($today.' 23:59:59');
			$getDateSum = CQuery($sql2);
			if($getDateSum['moneys'] > 2000){
				dataFormat('',0,'今日购买额度已满');
			}
			$goodsCollect = explode(',',I('get.params'));
			foreach($goodsCollect AS $val){
				$dataVal = explode('_',$val);
				$getGoodsMsg = M('GoodsEntity')->field('goods_price,goods_title,goods_pic,goods_attr,kcun,taxes')
							 ->where(array('goods_id'=>$dataVal[0]))->find();
				if($getGoodsMsg['taxes'] == '0'){
					$priceNum += $getGoodsMsg['goods_price']*$dataVal[1];
				}
				if(!$getGoodsMsg){
					dataFormat('',0,'商品参数异常');
				}else if($dataVal[1] > $getGoodsMsg['kcun']){
					dataFormat('',0,'商品库存不足');
				}
			}
			if($priceNum > 2000){
				dataFormat('',0,'单笔订单金额不能超过2000元');
			}
			if($getYearSum['moneys'] + $priceNum > 20000){
				dataFormat('',0,'已超过本年可购买的限定额度');
			}
			if($getDateSum['moneys'] + $priceNum > 2000){
				dataFormat('',0,'已超过今日可购买的限定额度');
			}
			dataFormat(I('get.params'),1,'success');
		}else{
			dataFormat('',0,'订单参数缺失');
		}
	}
	
    /*
	 * 分销商城_购物车删除
	 * @date: 2016-3-16 下午6:00:26
	 * @editor: XING
	 */
	public function remove(){
		$putDelete = M('ShopCart')
				   ->where(array('goods_id'=>I('get.id'),'cust_id'=>session('loginUid')))->delete();
		if($putDelete){
			dataFormat('',1,'商品已删除');
		}else{
			dataFormat('',0,'操作失败');
		}
	}

	/*
	 * 分销商城_购物车商品数量
	 * @date: 2016-05-06 11:45:00
	 * @editor: WENCUN
	 * */
	public function cartSum(){
		$shopCart = M('ShopCart');
		if(I('get.type') == '2'){
			$shopCart = $shopCart->where(array('biz_cust_id'=>spotSek(I('get.sek'))));
		}
		$cartSum = $shopCart
				 ->where(array('cust_id'=>session('loginUid'),'online'=>'1','type'=>I('get.type')))->sum('goods_num');
		if($cartSum>0){
			dataFormat($cartSum,1,'success');
		}else{
			dataFormat('',0,'没有商品');
		}
	}

	/*
	 * 分销商城_商城批量删除
	 * @date: 2016-04-20 13:00:00
	 * @editor: WENCUN
	 * */
	public function cartDel(){
		if(I('get.id')){
			$goodsId = substr(I('get.id'),0,strlen(I('get.id'))-1);
			$goods = explode('_',$goodsId);
			$cart = M('ShopCart');
			$cart->startTrans();
			foreach($goods as $value){
				$del = $cart->where(array('id'=>$value,'cust_id'=>session('loginUid')))->delete();
			}
			if($del){
				$cart->commit();
				dataFormat('',1,'删除成功');
			}else{
				$cart->rollback();
				dataFormat('',0,'操作失败');
			}
		}else{
			dataFormat('',0,'参数有误');
		}
	}
}